﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Expo.Training.BRule;
using Expo.Training.Entity;

/// <summary>
/// MasterPageBase 的摘要说明
/// </summary>
public class MasterPageBase : MasterPage
{
    public MasterPageBase()
    {
        
    }

    public string ResolveCommonImagePath(string imagePath)
    {
        return this.ResolveClientUrl("~/App_Themes/Common/Images/" + imagePath);
    }

    public string ResolveThemeImagePath(string imagePath)
    {
        return this.ResolveClientUrl("~/App_Themes/Winter_cs/Images/" + imagePath);
    }
    public string ResolveFilePath(string filePath)
    {
        return this.ResolveClientUrl("~/" + filePath);
    }

    public string ThemePath(string imagePath)
    {
        string theme = CookieManager.ReadCookie("PortalTheme");
        if (theme == null)
        {
            string dbCss = this.GetCLString("Portal.Css");
            if (!string.IsNullOrEmpty(dbCss))
            {
                theme = dbCss + "_cs";
            }
            else
            {
                theme = "classic_cs";
            }
        }

        return Base.Source + "/images/" + theme + "/" + imagePath;
    }

    public string FilePath(string filePath)
    {
        return this.ResolveClientUrl("~/" + filePath);
    }

    /// <summary>
    /// 获取CustomerCode
    /// </summary>
    public string CustomerCode
    {
        get
        {
            string customerCode = "";
            if (CookieManager.ReadCookie("CustomerCode") != null && CookieManager.ReadCookie("CustomerCode").Length > 0)
                customerCode = CookieManager.ReadCookie("CustomerCode");
            else
                customerCode = System.Configuration.ConfigurationManager.AppSettings.Get("DefaultCustomer");
            return customerCode.ToUpper();
        }
    }

    /// <summary>
    /// 返回customer类
    /// </summary>
    private Expo.Training.BRule.Config.CustomerConfig customer;
    public Expo.Training.BRule.Config.CustomerConfig Customer
    {
        get
        {
            if (this.customer == null)
            {
                this.customer = new Expo.Training.BRule.Config.CustomerConfig(this.CustomerCode);
            }
            return this.customer;
        }
    }

    public void GetLogoAttribute(string logo, ref string attrName, ref string attrValue)
    {
        string path = Server.MapPath(logo);
        if (File.Exists(path))
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            int width = image.Width;
            int height = image.Height;
            if (height > 61 && width <= 500)
            {
                attrName = "height";
                attrValue = "61";
            }
            else if (width > 500 && height <= 61)
            {
                attrName = "width";
                attrValue = "500";
            }
            else if (height > 61 && width > 500)
            {
                if ((width / height) > (500 / 61))
                {
                    attrName = "width";
                    attrValue = "500";
                }
                else
                {
                    attrName = "height";
                    attrValue = "61";
                }
            }
        }
    }

    public string ResolveCustomerLogoPath(string imagePath)
    {
        if (File.Exists(Server.MapPath("~/Data/CustomerSkin/" + this.CustomerCode + "/" + imagePath)))
        {
            return "~/Data/CustomerSkin/" + this.CustomerCode + "/" + imagePath;
        }
        else
        {
            return "~/Data/CustomerSkin/" + System.Configuration.ConfigurationManager.AppSettings.Get("DefaultCustomer") + "/Logo.jpg";
        }
    }

    private Member_Info member;
    public Member_Info Member
    {
        get
        {
            if (this.member == null || this.member.MemberID.ToString().Length == 0)
            {
                Member_Info objMember = new Member_Info();
                objMember.MemberID = Guid.Empty;

                if (Expo.Training.BRule.CookieManager.ReadCookie("MemberID") != null && Expo.Training.BRule.CookieManager.ReadCookie("MemberID").Length != 0)
                {
                    string strMemberID = Expo.Training.BRule.CookieManager.ReadCookie("MemberID");
                    objMember.MemberID = new Guid(strMemberID);
                    if (!objMember.Exist())
                    {
                        objMember.MemberID = Guid.Empty;
                    }
                }
                this.member = objMember;
            }

            return this.member;
        }
    }

    private Expo.Training.BRule.Environment currentEnvironment;
    public Expo.Training.BRule.Environment CurrentEnvironment
    {
        get
        {
            if (currentEnvironment == null)
            {
                currentEnvironment = new Expo.Training.BRule.Environment();
                if (this.Member.MemberID != Guid.Empty)
                {
                    currentEnvironment.MemberID = this.Member.MemberID;
                }
                currentEnvironment.CustomerCode = this.CustomerCode;
                currentEnvironment.Area = Expo.Training.BRule.Area.AreaLayer.Instance.GetAreaName();
                currentEnvironment.AreaCN = Expo.Training.BRule.Area.AreaLayer.Instance.GetAreaName(Expo.Training.BRule.Area.AreaLayer.AreaNameType.Chinese);
                try { currentEnvironment.ServerName = ConfigurationManager.AppSettings.Get("ServerName"); }
                catch { }
                try
                {
                    if (CookieManager.ReadCookie("DevModeCookie") != null && CookieManager.ReadCookie("DevModeCookie").Length > 0)
                        currentEnvironment.DevMode = Boolean.Parse(CookieManager.ReadCookie("DevModeCookie"));
                }
                catch { }
                try
                {
                    if (CookieManager.ReadCookie("LanguageCookie") != null && CookieManager.ReadCookie("LanguageCookie").Length > 0)
                        currentEnvironment.Language = CookieManager.ReadCookie("LanguageCookie");
                }
                catch { }
            }
            return this.currentEnvironment;
        }
    }

    public int GetCLInt(string keyName)
    {
        return Expo.Training.BRule.Config.ConfigLayer.Instance.GetInt(keyName, this.CurrentEnvironment);
    }
    public bool GetCLBool(string keyName)
    {
        return Expo.Training.BRule.Config.ConfigLayer.Instance.GetBool(keyName, this.CurrentEnvironment);
    }
    public string GetCLString(string keyName)
    {
        return Expo.Training.BRule.Config.ConfigLayer.Instance.GetString(keyName, this.CurrentEnvironment);
    }


    /**根据用户的部门ID判断首页跳转链接和顶部文字 2018.10.19**/
    private Guid memberDepartmentID = Guid.Empty;
    public Guid MemberDepartmentID
    {
        get
        {
            if (memberDepartmentID == Guid.Empty)
            {
                if (this.Member != null && this.Member.MemberID != Guid.Empty)
                {
                    Member_Department objD = new Member_Department();
                    objD.MemberID = this.Member.MemberID;
                    if (objD.Exist())
                    {
                        memberDepartmentID = objD.DepartmentID;
                    }
                }
            }
            return memberDepartmentID;
        }
    }

    public string GetHomeUrl()
    {
        //获取巴马教育局部门ID
        Guid _bm_departmentID;
        Expo.Training.Entity.Department_Info _bm_department = new Expo.Training.Entity.Department_Info();
        _bm_department.DepartmentName_LIKE = "巴马";
        _bm_department.Exist();
        _bm_departmentID = _bm_department.DepartmentID;

        Expo.Training.Entity.Department_Info _user_department = new Expo.Training.Entity.Department_Info();
        _user_department.DepartmentID = this.MemberDepartmentID;

        if (_user_department.Exist())
        {
            if (_user_department.KeyString.Contains(_bm_departmentID.ToString()))
            {
                return "/portal/bm/default.aspx";
            }
            else
            {
                return "/portal/xb/default.aspx";
            }
        }
        else
        {
            return "/portal/xb/default.aspx";
        }

    }

    public string GetTopTitle()
    {
        //获取巴马教育局部门ID
        Guid _bm_departmentID;
        Expo.Training.Entity.Department_Info _bm_department = new Expo.Training.Entity.Department_Info();
        _bm_department.DepartmentName_LIKE = "巴马";
        _bm_department.Exist();
        _bm_departmentID = _bm_department.DepartmentID;

        Expo.Training.Entity.Department_Info _user_department = new Expo.Training.Entity.Department_Info();
        _user_department.DepartmentID = this.MemberDepartmentID;

        if (_user_department.Exist())
        {
            if (_user_department.KeyString.Contains(_bm_departmentID.ToString()))
            {
                return "<span style=\"background:none; padding-left:0;\">中国</span><span>99学吧</span><span>市民学习网</span>";
            }
            else
            {
                return "<span style=\"background:none; padding-left:0;\">中国</span><span>慈溪</span><span>市民终身学习门户网站</span>";
            }
        }
        else
        {
            return "<span style=\"background:none; padding-left:0;\">中国</span><span>慈溪</span><span>市民终身学习门户网站</span>";
        }

    }
}
